function initFormDataFromUrl(form) {
    var urlParams = {};
    var match,
        regex = /([^&=]+)=([^&]*)/g;
    var searchString = window.location.search.substring(1);
    while ((match = regex.exec(searchString))) {
        var key = decodeURIComponent(match[1]);
        var value = decodeURIComponent(match[2]);
        urlParams[key] = value;
    }
    setKeyValue(form,"",urlParams);
}

//根据id获取数据并初始化表单
function initFormData(url, data, form, afterCallback, beforeCallback) {
    AsyncGet(url, data, function (value) {
        data = value.data || value;
        if (beforeCallback != null) beforeCallback(data);
        setKeyValue(form,"",data);
        if (afterCallback != null) afterCallback(data);
    });
}

function setKeyValue(form,prefix,data) {
    for (var i in data) {
        var value=data[i];
        if(value==null) continue;
        if(value.constructor===Object){
            setKeyValue(form,prefix==""?i:prefix+"."+i,value);
        }else if(value.constructor===Array){
            for(var j=0;j< value.length;j++){
                setKeyValue(form,prefix+"."+i+"["+j+"]",value[j]);
            }
        }else {
            var key = prefix==""?i:prefix+"."+i;
            if ($(form).find("[name='" + key + "']").is("input[name]")) {
                $(form).find("[name='" + key + "']").val(value);
            }
            if ($(form).find("[name='" + key + "']").is("span")) {
                $(form).find("[name='" + key + "']").text(value);
            }
            if ($(form).find("[name='" + key + "']").is("select")) {
                if (value != null) {
                    if ($(form).find("[name='" + key + "']").parent().find("select").length != 1) {
                        value = value + "";
                        var length = $(form).find("[name='" + key + "']").parent().find("select").length;
                        for (var j = 0; j < length; j++) {
                            var select = $(form).find("[name='" + key + "']").parent().find("select:eq(" + j + ")");
                            var string = $(select).find("option:last").val() + "";
                            $(select).val(value.substr(0, string.length)).change();
                        }
                    } else {
                        $(form).find("[name='" + key + "']").val(value).change();
                    }
                }
            }
            if ($(form).find("[name='" + key + "']").is("textarea")) {
                $(form).find("[name='" + key + "']").val(value);
            }
        }
    }
}

function append(data, param) {
    if (data instanceof FormData) {
        for (var key in param) {
            data.append(key, param[key]);
        }
    } else $.extend(data, param);
}

function getFormData(form) {
    if ($(form).has("input:file").length > 0) return getFileFormData(form);
    var data = {};
    $(form).find("input[name],select[name],textarea[name]").each(function (index, value) {
        data[$(this).attr("name")] = $(this).val();
    });
    return data;
}

function getFileFormData(form) {
    var formData = new FormData();
    $(form).find("input[name],select[name],textarea[name]").each(function (index, value) {
        if ($(this).is("[type='file']")) {
            for (var i = 0; i < this.files.length; i++) {
                formData.append($(this).attr("name"), this.files[i]);
            }

        } else {
            if ($(this).val() != null) {
                formData.append($(this).attr("name"), $(this).val());
            }
        }
    });
    return formData;
}

function clearFormData(form) {
    $(form).find("input[name],select[name],textarea[name]").each(function () {
        $(this).val("");
        if ($(this).is("select.select2")) $(this).empty();
    })
}